Skip to content

Player Counter Queries#119

Closed
AdmiralScoob wants to merge 2 commits intopelican-dev:mainfrom
AdmiralScoob:patch-1
Closed

Player Counter Queries#119
AdmiralScoob wants to merge 2 commits intopelican-dev:mainfrom
AdmiralScoob:patch-1

Conversation

@AdmiralScoob
Copy link
Copy Markdown

@AdmiralScoob AdmiralScoob commented Apr 5, 2026

Modified "runQuery" and "canRunQuery" to use gethostbyname with the allocation alias to fix issues with non-IP container aliases.

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced hostname alias resolution for server queries. When alias configuration is enabled, the system now properly resolves hostnames to their corresponding IP addresses, improving query reliability.

Copilot AI review requested due to automatic review settings April 5, 2026 00:34
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 5, 2026

📝 Walkthrough

Walkthrough

The IP/host selection logic in GameQuery::runQuery() and GameQuery::canRunQuery() now resolves allocation aliases to IP addresses via gethostbyname() when the player-counter.use_alias configuration is enabled, rather than using the raw alias value directly.

Changes

Cohort / File(s) Summary
Alias IP Resolution
player-counter/src/Models/GameQuery.php
Modified IP/host selection logic to resolve $allocation->alias via gethostbyname() and use the resolved hostname's IP (after is_ip validation) when player-counter.use_alias is enabled.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 An alias now finds its true IP address,
Through gethostbyname() we resolve the mess,
No more raw hostnames in our game query calls,
Resolved IPs connect us through all the walls! ✨

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'Player Counter Queries' is vague and generic, failing to convey the specific technical change of implementing gethostbyname resolution for allocation aliases. Use a more descriptive title such as 'Resolve allocation alias via gethostbyname in GameQuery' or 'Fix Player Counter queries with non-IP container aliases' to clearly communicate the main technical change.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the player-counter query logic to support allocation aliases that are hostnames (non-IP container aliases) by resolving the alias to an IP address before running queries.

Changes:

  • Resolve Allocation::$alias via gethostbyname() when player-counter.use_alias is enabled.
  • Apply the resolved IP in both runQuery() and canRunQuery() selection logic.

Comment on lines +42 to 43
$ip = config('player-counter.use_alias') && is_ip(gethostbyname($allocation->alias)) ? gethostbyname($allocation->alias) : $allocation->ip;
$ip = is_ipv6($ip) ? '[' . $ip . ']' : $ip;
Copy link

Copilot AI Apr 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gethostbyname($allocation->alias) is called twice in the same expression, which performs duplicate DNS lookups and could even return different results if DNS changes between calls. Resolve the hostname once into a local variable and reuse it for both the is_ip() check and the selected $ip value.

Copilot uses AI. Check for mistakes.
Comment on lines +57 to 59
$ip = config('player-counter.use_alias') && is_ip(gethostbyname($allocation->alias)) ? gethostbyname($allocation->alias) : $allocation->ip;

return !in_array($ip, ['0.0.0.0', '::']);
Copy link

Copilot AI Apr 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

canRunQuery() now performs a DNS lookup via gethostbyname(). If this method is used in lists/loops (e.g., rendering tables of servers/allocations), it can block request handling and add significant latency. Consider keeping canRunQuery() as a cheap check (e.g., only validate placeholder IPs / presence of alias) and perform DNS resolution in runQuery() (or cache the resolved value).

Copilot uses AI. Check for mistakes.
Comment on lines +42 to 43
$ip = config('player-counter.use_alias') && is_ip(gethostbyname($allocation->alias)) ? gethostbyname($allocation->alias) : $allocation->ip;
$ip = is_ipv6($ip) ? '[' . $ip . ']' : $ip;
Copy link

Copilot AI Apr 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gethostbyname() only resolves IPv4 (A) records. If an allocation alias resolves exclusively to an IPv6 (AAAA) address, gethostbyname() returns the hostname unchanged, is_ip() will be false, and the code will fall back to $allocation->ip—which may reintroduce the original “alias not usable” issue for IPv6-only setups. Consider using a resolver that supports AAAA (e.g., dns_get_record() for A/AAAA) and then applying the existing IPv6 bracket logic.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@player-counter/src/Models/GameQuery.php`:
- Line 57: The line currently does redundant DNS lookups with
gethostbyname($allocation->alias) and duplicates logic used elsewhere; extract a
private helper (e.g., resolveAllocationIp or resolveAliasIp) that accepts
$allocation (or alias and fallback ip) and implements: check
config('player-counter.use_alias'), call gethostbyname only once, validate with
is_ip, and return the resolved IP or $allocation->ip; then replace both inline
usages (including the occurrence at line 42) with calls to that helper to
centralize logic and avoid double lookups.
- Around line 42-43: The code uses gethostbyname() to resolve $allocation->alias
which only returns IPv4, so IPv6 aliases won't be detected and fall back to
$allocation->ip; replace the gethostbyname() call with an IPv6-aware resolution
(e.g., check DNS_AAAA records via dns_get_record($allocation->alias, DNS_AAAA)
or use socket_addrinfo_lookup($allocation->alias)) and pick the first valid
IPv6/IPv4 address, then run the existing is_ip() and is_ipv6() logic to set $ip
(keeping the IPv6 bracket formatting). Ensure you update the resolution logic
where $ip is assigned (the lines referencing gethostbyname, is_ip, is_ipv6 and
$allocation->alias) so it handles both A and AAAA responses.
- Line 42: The current expression calls gethostbyname($allocation->alias) twice
causing redundant DNS lookups; compute and store the resolved value once (e.g.
$resolved = gethostbyname($allocation->alias)) and then use that cached
$resolved in the conditional is_ip(...) and in the ternary result when setting
$ip, keeping the existing config('player-counter.use_alias') check and falling
back to $allocation->ip when not an IP.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9160de54-a8cd-4fdc-86f8-6dbf0640b966

📥 Commits

Reviewing files that changed from the base of the PR and between 5157d2f and 29cbb83.

📒 Files selected for processing (1)
  • player-counter/src/Models/GameQuery.php
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Agent

Comment on lines +42 to 43
$ip = config('player-counter.use_alias') && is_ip(gethostbyname($allocation->alias)) ? gethostbyname($allocation->alias) : $allocation->ip;
$ip = is_ipv6($ip) ? '[' . $ip . ']' : $ip;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🌐 Web query:

Does PHP gethostbyname support IPv6 addresses?

💡 Result:

No, PHP's gethostbyname does not support returning IPv6 addresses. It is explicitly documented as returning the IPv4 address corresponding to a given Internet host name. On failure to resolve an IPv4 address, it returns the unmodified hostname string. The official PHP manual for current versions (PHP 8+) states: "gethostbyname — Get the IPv4 address corresponding to a given Internet host name" and "Returns the IPv4 address or a string containing the unmodified hostname on failure." This documentation has been consistent across PHP 4, 5, 7, and 8. User notes and community workarounds confirm it does not query AAAA (IPv6) records; custom functions using dns_get_record(DNS_AAAA) are recommended for IPv6. Long-standing feature requests and bugs (e.g., PHP bug #49493 since 2009, doc bug #33170) highlight the lack of IPv6 support, with no resolution in mainline PHP as of 2026. Recent internal changes (e.g., PR #15555 in 2024 converting to getaddrinfo) modernize the underlying implementation but preserve the IPv4-only return behavior to maintain compatibility, as noted in discussions. For IPv6 resolution, use dns_get_record($host, DNS_AAAA) or socket_addrinfo_lookup (PHP 7.2+).

Citations:


gethostbyname() does not support IPv6 alias resolution.

PHP's gethostbyname() only returns IPv4 addresses. If the alias resolves to an IPv6 address, it returns the hostname unchanged, which fails the is_ip() check and falls back to $allocation->ip. Since line 43 handles IPv6 formatting, consider using dns_get_record($allocation->alias, DNS_AAAA) or socket_addrinfo_lookup() to support IPv6 alias resolution.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@player-counter/src/Models/GameQuery.php` around lines 42 - 43, The code uses
gethostbyname() to resolve $allocation->alias which only returns IPv4, so IPv6
aliases won't be detected and fall back to $allocation->ip; replace the
gethostbyname() call with an IPv6-aware resolution (e.g., check DNS_AAAA records
via dns_get_record($allocation->alias, DNS_AAAA) or use
socket_addrinfo_lookup($allocation->alias)) and pick the first valid IPv6/IPv4
address, then run the existing is_ip() and is_ipv6() logic to set $ip (keeping
the IPv6 bracket formatting). Ensure you update the resolution logic where $ip
is assigned (the lines referencing gethostbyname, is_ip, is_ipv6 and
$allocation->alias) so it handles both A and AAAA responses.

}

$ip = config('player-counter.use_alias') && is_ip($allocation->alias) ? $allocation->alias : $allocation->ip;
$ip = config('player-counter.use_alias') && is_ip(gethostbyname($allocation->alias)) ? gethostbyname($allocation->alias) : $allocation->ip;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Double gethostbyname() call causes redundant DNS lookups and potential inconsistency.

gethostbyname($allocation->alias) is called twice—once in the condition and once in the result. This triggers two DNS queries per invocation, which is inefficient and could yield inconsistent results if DNS changes between calls.

🔧 Proposed fix: cache the resolved hostname
-        $ip = config('player-counter.use_alias') && is_ip(gethostbyname($allocation->alias)) ? gethostbyname($allocation->alias) : $allocation->ip;
+        $resolvedAlias = $allocation->alias ? gethostbyname($allocation->alias) : null;
+        $ip = config('player-counter.use_alias') && is_ip($resolvedAlias) ? $resolvedAlias : $allocation->ip;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@player-counter/src/Models/GameQuery.php` at line 42, The current expression
calls gethostbyname($allocation->alias) twice causing redundant DNS lookups;
compute and store the resolved value once (e.g. $resolved =
gethostbyname($allocation->alias)) and then use that cached $resolved in the
conditional is_ip(...) and in the ternary result when setting $ip, keeping the
existing config('player-counter.use_alias') check and falling back to
$allocation->ip when not an IP.

}

$ip = config('player-counter.use_alias') && is_ip($allocation->alias) ? $allocation->alias : $allocation->ip;
$ip = config('player-counter.use_alias') && is_ip(gethostbyname($allocation->alias)) ? gethostbyname($allocation->alias) : $allocation->ip;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Same double DNS lookup issue and duplicated logic.

This line has the same redundant gethostbyname() calls as line 42. Consider extracting a shared helper method to avoid duplication and ensure consistent behavior.

🔧 Proposed fix: extract helper method and cache DNS result

Add a private helper method and refactor both usages:

+    private static function resolveIp(Allocation $allocation): string
+    {
+        if (config('player-counter.use_alias') && $allocation->alias) {
+            $resolved = gethostbyname($allocation->alias);
+            if (is_ip($resolved)) {
+                return $resolved;
+            }
+        }
+
+        return $allocation->ip;
+    }
+
     /** `@return` ?array{hostname: string, map: string, current_players: int, max_players: int, players: ?array<array{id: string, name: string}>} */
     public function runQuery(Allocation $allocation): ?array
     {
         if (!static::canRunQuery($allocation)) {
             return null;
         }

-        $ip = config('player-counter.use_alias') && is_ip(gethostbyname($allocation->alias)) ? gethostbyname($allocation->alias) : $allocation->ip;
+        $ip = static::resolveIp($allocation);
         $ip = is_ipv6($ip) ? '[' . $ip . ']' : $ip;
     public static function canRunQuery(?Allocation $allocation): bool
     {
         if (!$allocation) {
             return false;
         }

-        $ip = config('player-counter.use_alias') && is_ip(gethostbyname($allocation->alias)) ? gethostbyname($allocation->alias) : $allocation->ip;
+        $ip = static::resolveIp($allocation);

         return !in_array($ip, ['0.0.0.0', '::']);
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@player-counter/src/Models/GameQuery.php` at line 57, The line currently does
redundant DNS lookups with gethostbyname($allocation->alias) and duplicates
logic used elsewhere; extract a private helper (e.g., resolveAllocationIp or
resolveAliasIp) that accepts $allocation (or alias and fallback ip) and
implements: check config('player-counter.use_alias'), call gethostbyname only
once, validate with is_ip, and return the resolved IP or $allocation->ip; then
replace both inline usages (including the occurrence at line 42) with calls to
that helper to centralize logic and avoid double lookups.

@Boy132
Copy link
Copy Markdown
Member

Boy132 commented Apr 5, 2026

Thanks but the use_alias setting is not meant for domains.
It should be used when your allocation ip is private/local and the alias is set to a public ip.

@Boy132 Boy132 closed this Apr 5, 2026
@AdmiralScoob AdmiralScoob deleted the patch-1 branch April 5, 2026 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants